﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.DTOs;
using Hartley.ApplicationLogic;

namespace WebApplication1.AddPages
{
    public partial class AddMember : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ddlMemberDivision.SelectedIndex = 0;
            ddlMemberHouse.SelectedIndex =0;
            //ddlMemberMemberType.SelectedIndex = -1;
            ddlMemberOLyr.SelectedIndex = 0;
            ddlMemberSeqQuestion.SelectedIndex = 0;
            //ddlMemberStatus.SelectedIndex = -1;
            ddlMemberStudiedFrom.SelectedIndex = 0;
            ddlMemberStudiedUpTo.SelectedIndex = 0;
            ddlMemberTitle.SelectedIndex =2;
        }

        protected void btnMemberAdd_Click(object sender, EventArgs e)
         {
             if(ddlMemberDivision.SelectedIndex == 0)
             {
                 ddlMemberDivision.SelectedValue = "";
             }
             if (ddlMemberHouse.SelectedIndex == 0)
             {
                 ddlMemberHouse.SelectedValue = "";
             }
             if (ddlMemberOLyr.SelectedIndex == 0)
             {
                 ddlMemberOLyr.SelectedValue = "";
             }
             if (ddlMemberSeqQuestion.SelectedIndex == 0)
             {
                 ddlMemberSeqQuestion.SelectedValue = "";
             }
             if (ddlMemberStudiedFrom.SelectedIndex == 0)
             {
                 ddlMemberStudiedFrom.SelectedValue = "";
             }
             if (ddlMemberStudiedUpTo.SelectedIndex == 0)
             {
                 ddlMemberStudiedUpTo.SelectedValue = "";
             }
             if (ddlMemberTitle.SelectedIndex == 0)
             {
                 ddlMemberTitle.SelectedValue = "";
             }
            string  DOB = txtMemberDOB.Text == "" ? "1975-01-01" : txtMemberDOB.Text;
            MemberDTO memberDTO = new MemberDTO { MemberId = Guid.NewGuid(),
                                                     Title=ddlMemberTitle.SelectedValue,
                                                     FirstName=txtMemberFirstName.Text,
                                                     LastName=txtMemberLastName.Text,
                                                     DateOfBirth = Convert.ToDateTime(DOB),
                                                     TelePhoneNo=txtMemberTelephoneNumber.Text,
                                                     MobileNo = txtMemberMobileNumber.Text,
                                                     Email=txtMemberEmail.Text,                                                     
                                                     PermanantAddress=txtMemberPermAddress.Text,
                                                     TemporaryAddress=txtMemberTempAddress.Text,
                                                     OrdinaryBatchYear=ddlMemberOLyr.SelectedValue,
                                                     Division=ddlMemberDivision.SelectedValue,
                                                     SchoolIndexNo=txtMemberIndexNumber.Text,
                                                     LifeMemberNo=txtMemberLifeNo.Text,
                                                     Designation=txtMemberDesignation.Text,
                                                     WorkingAt=txtMemberWorkingAt.Text,
                                                     StudiedFrom=ddlMemberStudiedFrom.SelectedValue,
                                                     StudiedUpto=ddlMemberStudiedUpTo.SelectedValue,
                                                     House=ddlMemberHouse.SelectedValue,
                                                     MemberType=hfMemberStatus.Value,
                                                     Status=hfMemberStatus.Value,
                                                     UserName=txtMemberUserName.Text,
                                                     Password=txtMemberPassword.Text,
                                                     PhotoPath=fuMemberPhoto.FileName,
                                                     SecurityQuestion=ddlMemberSeqQuestion.SelectedValue,
                                                     SecurityAnswer=txtSeqAnswer.Text,
                                                     Description=txtMemberDesc.Text,
                                                     //ModifiedBy = "Tharsan",
                                                     ModifiedTime = DateTime.Now };
            MemberAL memberAL= new MemberAL();
            memberAL.saveMember(memberDTO);

            if ((fuMemberPhoto.PostedFile != null) && (fuMemberPhoto.PostedFile.ContentLength > 0))
            {
                string fileName = System.IO.Path.GetFileName(fuMemberPhoto.PostedFile.FileName);
                string saveLocation = Server.MapPath("memberPhotos")+"\\"+fileName;
                try
                {
                    fuMemberPhoto.PostedFile.SaveAs(saveLocation);
                    Response.Write("The File has been uploaded successfully");
                }
                catch (Exception ex)
                {
                    Response.Write("Error occured while uploading the files...!!!"+ex);
                }
            }
                else
                {
                    Response.Write("Please select a file to upload.");

                }

            }

        

        protected void btnCheckAvailabity_Click(object sender, EventArgs e)
        {
            MemberAL memberAL = new MemberAL();
            bool isUserNameAvailble = memberAL.checkAvailabilityOfUserName(txtMemberUserName.Text);
            if (isUserNameAvailble)
            {
                lblUserNameAvail.Text = "You may choose this user name";
            }
            else
            {
                lblUserNameAvail.Text = "There is already a similar user name exists.Please choose another one";
            }
        }
        }
        }

        
    


